﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameScene : MonoBehaviour {
    public GameObject snake;
    public float speedY = 600.0f;
    public float offsetY = -100.0f;
    Vector3 lastMousePos;

    public List<GameObject> blockList;
    public List<GameObject> blockPool;
    public GameObject BlockPrefab;

	// Use this for initialization
	void Start () {
        blockList = new List<GameObject>();
        blockPool = new List<GameObject>();

        for (int i = 0; i != 20; ++i) {
            GameObject go = Instantiate(BlockPrefab) as GameObject;
            blockPool.Add(go);
        }
	}
	
	// Update is called once per frame
	void Update () {
        inputUpdate();
        runAndHitUpdate(Time.deltaTime);
        recoverUpdate();
	}

    void inputUpdate() {
        if (Input.GetMouseButtonDown(0)) {
            lastMousePos = Input.mousePosition;
        }else if (Input.GetMouseButton(0)) {
            if (Vector3.Distance(Input.mousePosition, lastMousePos) > 0.1f) {
                Vector3 detlaPos = Input.mousePosition - lastMousePos;
                lastMousePos = Input.mousePosition;
                try2MoveByX(detlaPos.x);
            }    
        }
    }

    void runAndHitUpdate(float detlaTime) {
        float detlaY = speedY * detlaTime;
        snake.GetComponent<RectTransform>().anchoredPosition3D += new Vector3(0, detlaY, 0);
        float y = offsetY - snake.GetComponent<RectTransform>().anchoredPosition3D.y;
        snake.transform.parent.gameObject.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(0,y,0);
    }
    void try2MoveByX(float detlaX) {
        Vector3 oldPos = snake.GetComponent<RectTransform>().anchoredPosition3D;

        float xMin = -513.0f;
        float xMax = 513.0f;

        float x=snake.GetComponent<RectTransform>().anchoredPosition3D.x + detlaX;
        x = x < xMin ? xMin : x;
        x = x > xMax ? xMax : x;

        if (blockList.Count > 0) {
            for (int i = 0; i < blockList.Count; ++i){
                GameObject blockGo = blockList[i];
                float yMin = blockGo.GetComponent<RectTransform>().anchoredPosition3D.y - 216.0f / 2;
                float yMax = blockGo.GetComponent<RectTransform>().anchoredPosition3D.y + 216.0f / 2;
                if (oldPos.y >= yMin && oldPos.y <= yMax) {
                    float xLeft = blockGo.GetComponent<RectTransform>().anchoredPosition3D.x - 216.0f / 2 - 27.0f;
                    float xRight = blockGo.GetComponent<RectTransform>().anchoredPosition3D.x + 216.0f / 2 + 27.0f;
                    if (oldPos.x <= xLeft && oldPos.x + detlaX > xLeft) {
                        x = xLeft;
                    }
                    if (oldPos.x >= xRight && oldPos.x + detlaX < xRight){
                        x = xRight;
                    }
                }
            }
        }

        snake.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(x, oldPos.y, 0);
    }

    void recoverUpdate() {
        if (blockList.Count > 0) {
            for (int i = 0; i < blockList.Count; ++i){
                GameObject blockGo = blockList[i];
                if (blockGo.GetComponent<RectTransform>().anchoredPosition3D.y < snake.GetComponent<RectTransform>().anchoredPosition3D.y - 960.0f)
                {
                    blockGo.GetComponent<RectTransform>().anchoredPosition3D = Vector3.zero;
                    pushBlockPool(blockGo);
                    blockList.RemoveAt(i);
                    i--;
                }
            }
        }
    }
    public GameObject popBlockPool() {
        if (blockPool.Count <= 0) { 
            //
            return null;
        }
        GameObject block = blockPool[0];
        blockPool.RemoveAt(0);
        return block;
    }

    public void pushBlockPool(GameObject blockGo) {
        blockPool.Add(blockGo);
        return;
    }

}
